# Contributor: Leo <thinkabit.ukim@gmail.com>
# Maintainer: Simon Zeni <simon@bl4ckb0ne.ca>
pkgname=mingw-w64-gcc
pkgver=13.2.0
pkgrel=2
pkgdesc="Cross GCC for the MinGW-w64 cross-compiler"
url="https://gcc.gnu.org"
arch="x86_64 x86"
license="GPL-3.0-or-later"
depends="
	mingw-w64-binutils
	mingw-w64-crt
	mingw-w64-headers
	mingw-w64-winpthreads
	"
makedepends="
	gmp-dev
	isl-dev
	mpc1-dev
	mpfr-dev
	zlib-dev
	zstd-dev
	"
subpackages="$pkgname-ccache"
source="
	$pkgname-$pkgver.tar.xz::https://ftp.gnu.org/gnu/gcc/gcc-$pkgver/gcc-$pkgver.tar.xz
	0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch
	"
builddir="$srcdir/gcc-$pkgver"

case "$CARCH" in
x86_64)
	_targets="x86_64-w64-mingw32 i686-w64-mingw32"
	makedepends="
		$makedepends
		i686-mingw-w64-binutils
		i686-mingw-w64-crt
		i686-mingw-w64-headers
		i686-mingw-w64-winpthreads
		"
	subpackages="$subpackages i686-$pkgname:x32"
	;;
x86)
	_targets="i686-w64-mingw32"
	;;
esac

build() {
	# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100431
	export CFLAGS="$CFLAGS -O2 -Wno-error=format-security"
	export CXXFLAGS="$CXXFLAGS -O2 -Wno-error=format-security"

	local target
	for target in $_targets; do
		msg "building target: $target"
		(
		local bld="$srcdir"/bld-$target
		mkdir -p "$bld"
		cd "$bld"
		"$builddir"/configure \
			--prefix=/usr \
			--target=$target \
			--libexecdir=/usr/libexec \
			--disable-dw2-exceptions \
			--disable-multilib \
			--disable-nls \
			--disable-sjlj-exceptions \
			--disable-werror \
			--enable-checking=release \
			--enable-cloog-backend=isl \
			--enable-fully-dynamic-string \
			--enable-languages=c,lto,c++ \
			--enable-libgomp \
			--enable-libstdcxx-time=yes \
			--enable-lto \
			--enable-shared \
			--enable-static \
			--enable-threads=posix \
			--with-bugurl="https://gitlab.alpinelinux.org/alpine/aports/-/issues" \
			--with-system-zlib
		make
		)
	done
}

package() {
	local target
	for target in $_targets; do
		make -C "$srcdir"/bld-$target DESTDIR="$pkgdir" install
		$target-strip --strip-unneeded "$pkgdir"/usr/$target/lib/*.dll
		ln -s $target-gcc "$pkgdir"/usr/bin/$target-cc

		# Move DLLs
		mkdir -p "$pkgdir"/usr/$target/bin
		mv "$pkgdir"/usr/$target/lib/*.dll "$pkgdir"/usr/$target/bin
	done

	# Unnecessary files
	rm -r "$pkgdir"/usr/share
	rm "$pkgdir"/usr/lib/libcc1.*
}

x32() {
	pkgdesc="$pkgdesc (i686 target)"
	depends="
		i686-mingw-w64-binutils
		i686-mingw-w64-crt
		i686-mingw-w64-headers
		i686-mingw-w64-winpthreads
		"

	amove usr/i686-w64-mingw32
	amove usr/bin/i686-w64-mingw32-*
	amove usr/lib/gcc/i686-w64-mingw32
	amove usr/libexec/gcc/i686-w64-mingw32
}

ccache() {
	pkgdesc="$pkgdesc (ccache symlinks)"
	install_if="$pkgname=$pkgver-r$pkgrel ccache"

	mkdir -p "$subpkgdir"/usr/lib/ccache/bin

	local target tool
	for target in $_targets; do
		for tool in c++ cc cpp g++ gcc gcc-$pkgver; do
			ln -sfv ../../../bin/ccache "$subpkgdir"/usr/lib/ccache/bin/$target-$tool
		done
	done
}

sha512sums="
d99e4826a70db04504467e349e9fbaedaa5870766cda7c5cab50cdebedc4be755ebca5b789e1232a34a20be1a0b60097de9280efe47bdb71c73251e30b0862a2  mingw-w64-gcc-13.2.0.tar.xz
8266eae8b3be5557e8cd79c21b6326db7cd83604db6d6ad83923798650fcd8d0f9f1f4ed424c816a48e91f21fb1852ab9272c73100b219f07d3bbb566bfd6fae  0020-libgomp-Don-t-hard-code-MS-printf-attributes.patch
"
